//
//  MyHTTPTools.swift
//  MySwiftProduct09网络框架的封装
//
//  Created by apple on 2016/11/14.
//  Copyright © 2016年 WangChen. All rights reserved.
//

import UIKit
import AFNetworking

enum HTTPMethod:Int {
    case GET = 1
    case POST
}

class MyHTTPTools: AFHTTPSessionManager {

    static let sharedTool: MyHTTPTools = {
        let tool = MyHTTPTools()
        tool.responseSerializer.acceptableContentTypes?.insert("text/plain")
        return tool
    }()
    
    
    func request(requestMethod: HTTPMethod, urlStr: String, paramters: Any?, finish: @escaping ((Any?,Error?)->())) {
        
        let successCallBack = {
            (_:URLSessionDataTask, responseObj:Any?)->() in
            finish(responseObj,nil)
        }
        
        let failCallBack = {
            (_:URLSessionDataTask?, error:Error)->() in
            finish(nil,error)
            print(error)
        }
        
        
        if requestMethod == .GET{
            get(urlStr, parameters: paramters, progress: nil, success: successCallBack, failure: failCallBack)
        }else{
            post(urlStr, parameters: paramters, progress: nil, success: successCallBack, failure: failCallBack)
        }
        
        
    }
    
    
    
    
}



